Sir i want to know that -how to make autoincrement id in asp.net/C# with out using store-procedure in sql.. can it make by a Method or Function in .net ?.
Hi Sheen Buhay, Debug your code and check what value you have in dr[0].ToString(). I thing you might be getting Alphabets or symbols. Suggestion: Don't just copy and paste the code snippet provided in the forum as those are not generalize, those are specific set of code used to accomplish a specific task. Please do modify the code according to your requirement. Thanks
Hi Mr. Singh, I followed your snippet however I am getting an error "Input string was not in a correct format." Highlighted in red. Here's the snippet
if (dr.Read()) { string d = dr[0].ToString(); if (d == "") { txtstudid.Text = strTesda; } else { r = Convert.ToInt32(dr[0].ToString()); r = r + 1; txtstudid.Text = r.ToString(); }
Pawan are you talking about Auto Increment ID in SQLServer? If yes then follow these steps without creating any method.. 1.Open SQl Server Management Studio Express. 2.Go to your Database. 3.Rigth click on your Database Table Name. 4.Then Click on Modify. 5.In modify window click on your " ID" column Name. 6.Change its Data Type to int. 7.Then go to Column Prorerties below. 8.Under the "Indentity Specifications" nodes you will see (Is Indentity). 8.Set this property "Yes".
{ public void Autogenrate() { int r; try { con = new SqlConnection("data source=XYZ; user id=ABC; password=***; initial catalog=Detail"); con.Open(); SqlCommand cmd = new SqlCommand("Select max(id) from empp1", con); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { string d = dr[0].ToString(); if (d == "") { id.Text = "1001";//set the value in textbox which name is id } else { r = Convert.ToInt32(dr[0].ToString()); r = r + 1; id.Text = r.ToString(); } } con.Close(); con.Open(); cmd.CommandText = "insert empp1 values('" + id.Text + "','" + txtName.Text + "','" + txtCity.Text + "')"; cmd.ExecuteNonQuery(); } catch (Exception ex) { Response.Write("<script>alert("+ex.Message+")</script>"); } finally { con.Close(); }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
What if I change integer code as K001
please help me
Debug your code and check what value you have in dr[0].ToString(). I thing you might be getting Alphabets or symbols.
Suggestion: Don't just copy and paste the code snippet provided in the forum as those are not generalize, those are specific set of code used to accomplish a specific task. Please do modify the code according to your requirement.
Thanks
I followed your snippet however I am getting an error "Input string was not in a correct format." Highlighted in red.
Here's the snippet
Any idea why it causes this error? Thanks.
1.Open SQl Server Management Studio Express.
2.Go to your Database.
3.Rigth click on your Database Table Name.
4.Then Click on Modify.
5.In modify window click on your " ID" column Name.
6.Change its Data Type to int.
7.Then go to Column Prorerties below.
8.Under the "Indentity Specifications" nodes you will see (Is Indentity).
8.Set this property "Yes".
{public void Autogenrate()
{
int r;
try
{
con = new SqlConnection("data source=XYZ; user id=ABC; password=***; initial catalog=Detail");
con.Open();
SqlCommand cmd = new SqlCommand("Select max(id) from empp1", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string d = dr[0].ToString();
if (d == "")
{
id.Text = "1001";//set the value in textbox which name is id
}
else
{
r = Convert.ToInt32(dr[0].ToString());
r = r + 1;
id.Text = r.ToString();
}
}
con.Close();
con.Open();
cmd.CommandText = "insert empp1 values('" + id.Text + "','" + txtName.Text + "','" + txtCity.Text + "')";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("<script>alert("+ex.Message+")</script>");
}
finally
{
con.Close();
}